package com.my.xq.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.xq.domain.CourtRoleQueryParam;
import com.my.xq.domain.entity.CourtRole;
import com.my.xq.repository.CourtRoleRepository;
import com.sskj.core.annotation.ControllerLogExeTime;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/ywh")
public class CourtRoleServiceImpl {
	@Autowired
	CourtRoleRepository courtRoleRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/addywhInfo", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "添加业委会角色信息")
	public void addyzInfo(CourtRole p) {
		courtRoleRepository.save(p);
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/findAllNoPage", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询业委会角色信息不分页")
	public List<CourtRole> findAllNoPage() {
		return courtRoleRepository.findAll();
	}

	/**
	 * 获取委员职务
	 *
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/wyDuty", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询业委会委员职务")
	public List<CourtRole> findWyDuty() {
		return courtRoleRepository.findWyDuty();
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/findAll", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询业委会角色信息")
	@ApiOperation(value = "查询小区信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "roleName", value = "姓名", paramType = "form", dataType = "string"), @ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"), @ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<CourtRole> findAllPage(@ApiIgnore CourtRoleQueryParam pqp, @ApiIgnore Pageable pageable, HttpServletRequest request) {
		Page<CourtRole> page = courtRoleRepository.findPage(pqp, pageable);
		return page;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "查询一条业委会角色记录", log = false)
	@RequestMapping("/findOne")
	public CourtRole findOne(String id, HttpServletRequest request) {
		CourtRole court = courtRoleRepository.get(id);
		return court;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "删除业委会角色信息", log = false)
	@RequestMapping("/delywhInfo")
	public void delyz(String id) {
		courtRoleRepository.delete(id);
	}

	public List<String> toList(String value, String separator) {
		List<String> result = new ArrayList<String>();
		if (!this.isEmpty(value)) {
			String[] as = value.split(separator);
			for (int i = 0; i < as.length; i++) {
				result.add(as[i]);
			}
		}
		return result;
	}

	public boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
}
